package com.peoit.honeycomb.application.impl;

import com.peoit.honeycomb.application.BannerApplication;
import com.peoit.honeycomb.application.utils.ManageUtils;
import com.peoit.honeycomb.application.utils.ZqCustomException;
import com.peoit.honeycomb.domain.Banner;
import com.peoit.honeycomb.domain.repository.HibernateEntityRepository;
import com.peoit.honeycomb.dto.BannerDto;
import com.peoit.honeycomb.dto.BannerPageDto;
import com.peoit.honeycomb.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片轮播
 * Created by zhouqiu on 2015/10/23.
 */
@Service
public class BannerApplicationImpl extends HibernateEntityRepository implements BannerApplication {

    @Override
    public List<BannerDto> findByWherePage(BannerPageDto dto) {

        int pageSize = dto.getPageSize();
        int offset = dto.getOffset();

        List<Object> parameters = new ArrayList<>();

        StringBuffer where = new StringBuffer("") ;

        if (!StringUtils.isEmpty(dto.getName())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" name like ? ");
            parameters.add("%"+dto.getName()+"%");
        }
        if (!StringUtils.isEmpty(dto.getTitle())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" title like ? ");
            parameters.add("%"+dto.getTitle()+"%");
        }
        if (!StringUtils.isEmpty(dto.getDescription())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" description like ? ");
            parameters.add("%"+dto.getDescription()+"%");
        }

        if(where.length()>0){
            where.append(" AND ");
        }
        where.append(" 1=1 order by createTime desc");

        List<Banner> banners = queryByWherePage(Banner.class, where.toString(), CommonUtil.listToArray(parameters), offset, pageSize);
        List<BannerDto> bannerDtos = BannerDto.toPageList(banners);
        return  bannerDtos;
    }

    @Override
    public Long findPersonCount(BannerPageDto dto) {
        List<Object> parameters = new ArrayList<>();

        StringBuffer where = new StringBuffer("") ;

        if (!StringUtils.isEmpty(dto.getName())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" name like ? ");
            parameters.add("%"+dto.getName()+"%");
        }
        if (!StringUtils.isEmpty(dto.getTitle())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" title like ? ");
            parameters.add("%"+dto.getTitle()+"%");
        }
        if (!StringUtils.isEmpty(dto.getDescription())){
            if(where.length()>0){
                where.append(" AND ");
            }
            where.append(" description like ? ");
            parameters.add("%" + dto.getDescription() + "%");
        }

        if(where.length()>0){
            where.append(" AND ");
        }
        where.append(" 1=1 order by createTime desc");
        return getCountByWhere(Banner.class, where.toString(), CommonUtil.listToArray(parameters));
    }

    @Override
    @Transactional
    public boolean addBanner(BannerDto dto) {
        List<BannerDto> bannerBy = this.findBannerBy(dto.getCity());
        if(bannerBy.size()>=5){
            throw  new ZqCustomException("一个城市只能最多有5张广告图！！");
        }
        Banner banner = BannerDto.toEntity(dto);
        banner.setId(ManageUtils.getNumber());
        create(banner);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteBy(Class clas , String id) {
        String sql = "delete from "+clas.getName()+" where id = ?";
        Object[] params = { id };
        this.updateByFullJPQL(sql, params);
        return true;
    }

    @Override
    public List<BannerDto> findAllBy(String city) {
        StringBuffer where = new StringBuffer("") ;

        Object[] queryParams = new  Object[1];
        if (!StringUtils.isEmpty(city)){
            where.append(" city like ? ");
            queryParams[0] = city;
        }else {
            queryParams = null;
        }

        if(where.length()>0){
            where.append(" AND ");
        }
        where.append(" 1=1 order by createTime desc");
        List list = this.queryByWhereList(Banner.class, where.toString(), queryParams);
        List<BannerDto> bannerDtos = BannerDto.toPageList(list);
        return bannerDtos;
    }

    public List<BannerDto> findBannerBy(String city) {
        StringBuffer where = new StringBuffer("") ;

        where.append(" city like ?  order by createTime desc");
        Object[] queryParams = {city};

        List list = this.queryByWhereList(Banner.class, where.toString(), queryParams);
        List<BannerDto> bannerDtos = BannerDto.toPageList(list);
        return bannerDtos;
    }
}
